unit CDefine;

interface

uses SYSUtils,fileCtrl,Windows;

{ StdMax = 20;
      CurveMax = 100;
      CurvePath = 'c:\HHH\Curve\';
      OffDataPath = 'c:\HHH\OffData\';
      OnDataPath =  'c:\HHH\OnData\';
      Itemmax = 1440;
      DotNum = 40;}
const
     testflag = false;
     lpOverlapped : LPOVERLAPPED = nil;

var 
    IL0A,IH0A,ILbA,IHbA,k1A,k2A,k3A,k4A,WhA,wlA,weA,wsA : real;
    IL0B,IH0B,ILbB,IHbB,k1B,k2B,k3B,k4B,WhB,wlB,weB,wsB : real;

    IL0C,IH0C,ILbC,IHbC,k1C,k2C,k3C,k4C,WhC,wlC,weC,wsC : real;
    IL0D,IH0D,ILbD,IHbD,k1D,k2D,k3D,k4D,WhD,wlD,weD,wsD : real;

    {StdC1,StdR1 : array [0..Stdmax-1] of real; dateStr : String[20];
    StdC2,StdR2 : array [0..Stdmax-1] of real;
    Stdc20      : real; //the stand value of stand2
    BadP1,badP2 : Integer;
    Stddef      : Integer;
    Stddef1,stddef2 : Integer;
    Stand2Flag  : Boolean;
    fp1,fp2,fp3,fp4,fv,fm : real;}
    //fB,fV,fC,fD,fM : real;
    {Standind    : Integer;
    SelCurveNo  : Integer;
    CurveNoStr  : String;
    paraChange  : boolean;}

    Com1TMDef,Com1RepDef : integer;
    Com1tmind, Com1repind : Integer;
    Com1countL, Com1CountH  : array [0..4] of LongInt;
    OnLineFlagCom1  : Boolean;
    HandOKCom1      : Boolean;
    LowResetCom1    : Boolean;
    OutBufferCom1   : Array [0..5] of byte;
    InBufferCom1    : Array [0..12] of byte;
    OverCountCom1   : Integer;

    Com2TMDef,Com2RepDef : integer;
    Com2tmind, Com2repind : Integer;
    Com2countL, Com2CountH  : array [0..4] of LongInt;
    OnLineFlagCom2  : Boolean;
    HandOKCom2      : Boolean;
    LowResetCom2    : Boolean;
    OutBufferCom2   : Array [0..5] of byte;
    InBufferCom2    : Array [0..12] of byte;
    OverCountCom2   : Integer;

    Com3TMDef,Com3RepDef : integer;
    Com3tmind, Com3repind : Integer;
    Com3countL, Com3CountH  : array [0..4] of LongInt;
    OnLineFlagCom3  : Boolean;
    HandOKCom3      : Boolean;
    LowResetCom3    : Boolean;
    OutBufferCom3   : Array [0..5] of byte;
    InBufferCom3    : Array [0..12] of byte;
    OverCountCom3   : Integer;

    Com4TMDef,Com4RepDef : integer;
    Com4tmind, Com4repind : Integer;
    Com4countL, Com4CountH  : array [0..4] of LongInt;
    OnLineFlagCom4  : Boolean;
    HandOKCom4      : Boolean;
    LowResetCom4    : Boolean;
    OutBufferCom4   : Array [0..5] of byte;
    InBufferCom4    : Array [0..12] of byte;
    OverCountCom4   : Integer;

    OnLineFlag  : Boolean;
    HandOK      : Boolean;
    LowReset    : Boolean;
    OutBuffer   : Array [0..5] of byte;
    InBuffer    : Array [0..12] of byte;
    OverCount   : Integer;

    
    Com1, Com2, com3, com4: string;

function ResetLow: Boolean;
procedure CloseComm;
procedure SendOffline;
procedure SendOnLine;
procedure senddaout(ii,vv:integer);




function GetRValueCom1(ValL,valH:real):real;
function GetRhValueCom1(valH:real):real;
function GetCValueCom1(ValL,ValH:real):real;
//function GetCValueCom1(ValH,ValL:real):real;
function GetFValueCom1(CVaL:real):real;
function ResetLowCom1 : Boolean;
procedure CloseCommCom1;
function ReceiveCountCom1 : Boolean;
procedure SendOfflineCom1;
procedure SendOnLineCom1;
procedure senddaoutCom1(ii,vv:integer);

function GetRValueCom2(ValL,valH:real):real;
function GetRhValueCom2(valH:real):real;
function GetCValueCom2(ValL,ValH:real):real;
//function GetCValueCom2(valH,valL:real):real;
function GetFValueCom2(CVaL:real):real;
function ResetLowCom2 : Boolean;
procedure CloseCommCom2;
function ReceiveCountCom2 : Boolean;
procedure SendOfflineCom2;
procedure SendOnLineCom2;
procedure senddaoutCom2(ii,vv:integer);

function GetRValueCom3(ValL,valH:real):real;
function GetRhValueCom3(valH:real):real;
function GetCValueCom3(ValL,ValH:real):real;
//function GetCValueCom3(ValH,ValL:real):real;
function GetFValueCom3(CVaL:real):real;
function ResetLowCom3 : Boolean;
procedure CloseCommCom3;
function ReceiveCountCom3 : Boolean;
procedure SendOfflineCom3;
procedure SendOnLineCom3;
procedure senddaoutCom3(ii,vv:integer);

function GetRValueCom4(ValL,valH:real):real;
function GetRhValueCom4(valH:real):real;
function GetCValueCom4(ValL,ValH:real):real;
//function GetCValueCom4(valH,valL:real):real;
function GetFValueCom4(CVaL:real):real;
function ResetLowCom4 : Boolean;
procedure CloseCommCom4;
function ReceiveCountCom4 : Boolean;
procedure SendOfflineCom4;
procedure SendOnLineCom4;
procedure senddaoutCom4(ii,vv:integer);


implementation

//uses DataModuleMain;

var
    hCom1,hCom2, hCom3, hCom4    : THandle;
    PPCom1,PPCom2, PPCom3, PPCom4  : Integer;


procedure SendBuffer(Len:Integer);
begin
  WriteFile(hCom3, OutBuffer,Cardinal(Len),Cardinal(PPCom3),nil);
end;

Function ReadBuffer(Len:Integer) : Boolean;
var k,pp1 : Integer;
    inbuffer1 : array [0..11] of Byte;
begin
  ReadBuffer := false;
  LowReset := false;
  ReadFile(hCom3, InBuffer,Cardinal(Len),Cardinal(ppCom3),nil);
  if ppCom3>0 then begin
    if ppCom3<len then begin
      //sleep(100);
      ReadFile(hCom3,InBuffer1,Cardinal(len-ppCom3),Cardinal(pp1),nil);
      if (ppCom3+pp1)=Len then begin
        for k := 0 to pp1 - 1 do Inbuffer[k+ppCom3] := Inbuffer1[k];
        Readbuffer := true;
      end;
    end else ReadBuffer := true;
  end;
end;

procedure SendReset(rf:byte);
begin
  outbuffer[0] := $aa;
  outbuffer[1] := rf;
  outbuffer[2] := 0;
  outbuffer[3] := 0;
  outbuffer[4] := 0;
  outbuffer[5] := $aa;
  sendbuffer(6);
end;

procedure SendOffline;
begin
  outbuffer[0] := $bb;
  outbuffer[1] := Com1tmdef mod 100;
  outbuffer[2] := Com1tmdef div 100;
  outbuffer[3] := Com1repdef;
  outbuffer[4] := 10;
  outbuffer[5] := $bb;
  sendbuffer(6);
end;

procedure SendOnLine;
begin
  outbuffer[0] := $cc;
  outbuffer[1] := 0;  //1 calgs  0 no
  outbuffer[2] := 5;  // rep= rebhf[2] senddata
  outbuffer[3] := 0;
  outbuffer[4] := 0;
  outbuffer[5] := $cc;
  sendbuffer(6);
end;

procedure SendDAOut(ii,vv:Integer);
begin
  outbuffer[0] := $dd;
  outbuffer[1] := ii mod 100;
  outbuffer[2] := ii div 100;
  outbuffer[3] := vv mod 100;
  outbuffer[4] := vv div 100;
  outbuffer[5] := $dd;
  sendbuffer(6);
end;

function InitializeComm(SerialPort:Integer) : Boolean;
var dcb        : TDCB;
    TimeStatus : TCOMMTIMEOUTS;
    InitFlag   : Boolean;
    ComStr     : PChar;
begin
  if SerialPort=0 then ComStr := PChar(COM3);
  hCom3 := CreateFile(COMStr,
                     GENERIC_WRITE or GENERIC_READ,
                     0,
                     nil,
                     OPEN_EXISTING,
                     FILE_ATTRIBUTE_COMPRESSED,
                     0);
  InitFlag := (hCom3<>-1);
  if Initflag then begin
    InitFlag := GetCommState(hCOM3,dcb);
    if InitFlag then begin
      dcb.BaudRate := 2400;
      dcb.ByteSize := 8;
      dcb.Parity := NOPARITY;
      dcb.StopBits := ONESTOPBIT;
      InitFlag := SetCommState(HCOM3, dcb);
      GetCommTimeouts(HCOM3,TimeStatus);
      TimeStatus.ReadTotalTimeoutConstant := 500;
      SetCommTimeouts(HCOM3,TimeStatus);
    end;
  end;
  InitializeComm := InitFlag;
end;

function ResetLow : Boolean;
begin
  //if hcom<>-1 then closehandle(hcom);
  if hcom3<>0 then closehandle(hcom3);
  handok := false;
  if InitializeComm(0) then begin
    SendReset(1);
    sleep(1000);
    if readbuffer(2) then HandoK := (inbuffer[0]=$aa)and(inbuffer[1]=$aa);
    //else CloseHandle(Hcom);
  end;
  if not HandOK then begin
    if hcom3<>-1 then closehandle(hcom3);
    if InitializeComm(1) then begin
      SendReset(1);
      sleep(1000);
      if readbuffer(2) then HandoK := (inbuffer[0]=$aa)and(inbuffer[1]=$aa);
    end;
    if not HandOK then CloseHandle(Hcom3);
  end;
  if testflag then ResetLow := true
  else ResetLow := HandOK;
end;

procedure CloseComm;
begin
  sleep(1000);
  SendReset(0);
  CloseHandle(hCOM3);
  hcom1 := 0;
end;





{procedure Incppr;
begin
  inc(ppr,pfeed);
  with printer do begin
    if ppr>pageheight then begin
      ppr := 0;
      Newpage;
    end;
  end;
end;

function Addstrlen(k,l:Integer) : string;
var ts : string;
begin
  ts := inttostr(k);
  while length(ts)<l do ts := '0' + ts;
  Addstrlen := ts;
end;

function GetCurveName(CurveNo:Integer) : String;
begin
  GetCurveName := CoalNameStr+'.'+Addstrlen(curveno,2);
end;}

function GetRValueCom1(ValL,valH:real):real;
var temp : real;
begin
  if ValH<=IHbA then ValH:=IHbA+1;
  if IH0A<=IHbA then IH0A:=IHbA+1;
  temp := (ValH-IHbA)/(IH0A-IHbA) ;
  if temp>0 then temp := ln(temp)
  else temp := 1;
  if ValL<=ILbA then ValL:=ILbA+1;
  if (IL0A<=ILbA)then IL0A:=ILbA+1;
  GetRValueCom1 := ln((ValL-ILbA)/(IL0A-ILbA))/temp;
end;

function GetRhValueCom1(valH:real):real;
begin
  if ValH<=IHbA then ValH:=IHbA+1;
  if IH0A<=IHbA then IH0A:=IHbA+1;
  GetRhValueCom1 := ln((ValH-IHbA)/(IH0A-IHbA));
end;


//function GetCValueCom1(valH,valL:real):real;
function GetCValueCom1(valL,valH:real):real; 
var cc,rl,rh:real;
begin
  rl :=  GetRValueCom1(valL,valH);
  rh :=  GetRhValueCom1(valH);

  cc := k2A+k1A*rl+k4A+k3A*rh;
  if cc<0 then cc := 0;
  GetCValueCom1 := cc;
end;

function GetFValueCom1(CVal:real): real;
begin
    //GetFValue := (CVal*(1 + fC)+fB*fV+fD*fM)/0.239;
  {if cval>0 then getFValue := fp1+fp2*fv+fp3*CVal+fp4*fM
  else}
  getFValueCom1 := 0;
end;

procedure SendBufferCom1(Len:Integer);
begin
  WriteFile(hCom1, OutBufferCom1,Cardinal(Len),Cardinal(PPCom1),nil);
end;

Function ReadBufferCom1(Len:Integer) : Boolean;
var k,pp1 : Integer;
    inbuffer1 : array [0..11] of Byte;
begin
  ReadBufferCom1 := false;
  LowResetCom1 := false;
  ReadFile(hCom1, InBufferCom1,Cardinal(Len),Cardinal(ppCom1),nil);
  if ppCom1>0 then begin
    if ppCom1<len then begin
      //sleep(100);
      ReadFile(hCom1,InBuffer1,Cardinal(len-ppCom1),Cardinal(pp1),nil);
      if (ppCom1+pp1)=Len then begin
        for k := 0 to pp1 - 1 do InbufferCom1[k+ppCom1] := Inbuffer1[k];
        ReadbufferCom1 := true;
      end;
    end else ReadBufferCom1 := true;
  end;
end;

procedure SendResetCom1(rf:byte);
begin
  outbufferCom1[0] := $aa;
  outbufferCom1[1] := rf;
  outbufferCom1[2] := 0;
  outbufferCom1[3] := 0;
  outbufferCom1[4] := 0;
  outbufferCom1[5] := $aa;
  sendbufferCom1(6);
end;

procedure SendOfflineCom1;
begin
  outbufferCom1[0] := $bb;
  outbufferCom1[1] := Com1tmdef mod 100;
  outbufferCom1[2] := Com1tmdef div 100;
  outbufferCom1[3] := Com1repdef;
  outbufferCom1[4] := 10;
  outbufferCom1[5] := $bb;
  sendbufferCom1(6);
end;

procedure SendOnLineCom1;
begin
  outbufferCom1[0] := $cc;
  outbufferCom1[1] := 0;  //1 calgs  0 no
  outbufferCom1[2] := 5;  // rep= rebhf[2] senddata
  outbufferCom1[3] := 0;
  outbufferCom1[4] := 0;
  outbufferCom1[5] := $cc;
  sendbufferCom1(6);
end;

procedure SendDAOutCom1(ii,vv:Integer);
begin
  outbufferCom1[0] := $dd;
  outbufferCom1[1] := ii mod 100;
  outbufferCom1[2] := ii div 100;
  outbufferCom1[3] := vv mod 100;
  outbufferCom1[4] := vv div 100;
  outbufferCom1[5] := $dd;
  sendbufferCom1(6);
end;

function ReceiveCountCom1 : Boolean;
begin
  ReceiveCountCom1 := false;
  if (ReadBufferCom1(13)) then begin
    if (InbufferCom1[0]=$ee)and(InbufferCom1[12]=$ee) then begin
      Com1tmind := inbufferCom1[2]*100+inbufferCom1[1];
      Com1repind := inbufferCom1[3];
      Com1CountL[Com1repind] := inbufferCom1[4]*256*256*256+inbufferCom1[5]*256*256
                       +inbufferCom1[6]*256 + inbufferCom1[7];
      Com1CountH[Com1repind] := inbufferCom1[8]*256*256*256+inbufferCom1[9]*256*256
                       +inbufferCom1[10]*256 + inbufferCom1[11];
      if not onlineflagCom1 then begin
        Com1CountL[Com1repind] := round(Com1countL[Com1repind]*60/Com1tmdef);
        Com1CountH[Com1repind] := round(Com1countH[Com1repind]*60/Com1tmdef);
      end;
      ReceiveCountCom1 := true;
      if Com1CountL[Com1repind]=0 then Com1CountL[Com1repind] := 100000;
      if Com1CountH[Com1repind]=0 then Com1CountH[Com1repind] := 200000;
    end else if (InbufferCom1[11]=$ff) then LowResetCom1 := true;
  end;
  if testflag then begin
    Com1CountL[Com1repind] := 1000000+random(100000);
    Com1CountH[Com1repind] := 2000000+random(200000);
    ReceiveCountCom1 := true;
  end;
end;

function InitializeCommCom1(SerialPort:Integer) : Boolean;
var dcb        : TDCB;
    TimeStatus : TCOMMTIMEOUTS;
    InitFlag   : Boolean;
    ComStr     : PChar;
begin
  if SerialPort=0 then ComStr := PChar(COM1);
  hCom1 := CreateFile(COMStr,
                     GENERIC_WRITE or GENERIC_READ,
                     0,
                     nil,
                     OPEN_EXISTING,
                     FILE_ATTRIBUTE_COMPRESSED,
                     0);
  InitFlag := (hCom1<>-1);
  if Initflag then begin
    InitFlag := GetCommState(hCOM1,dcb);
    if InitFlag then begin
      dcb.BaudRate := 2400;
      dcb.ByteSize := 8;
      dcb.Parity := NOPARITY;
      dcb.StopBits := ONESTOPBIT;
      InitFlag := SetCommState(HCOM1, dcb);
      GetCommTimeouts(HCOM1,TimeStatus);
      TimeStatus.ReadTotalTimeoutConstant := 500;
      SetCommTimeouts(HCOM1,TimeStatus);
    end;
  end;
  InitializeCommCom1 := InitFlag;
end;

function ResetLowCom1 : Boolean;
begin
  //if hcom<>-1 then closehandle(hcom);
  if hcom1<>0 then closehandle(hcom1);
  handokCom1 := false;
  if InitializeCommCom1(0) then begin
    SendResetCom1(1);
    sleep(1000);
    if readbufferCom1(2) then HandoKCom1 := (inbufferCom1[0]=$aa)and(inbufferCom1[1]=$aa);
    //else CloseHandle(Hcom);
  end;
  if not HandOKCom1 then begin
    if hcom1<>-1 then closehandle(hcom1);
    if InitializeCommCom1(1) then begin
      SendResetCom1(1);
      sleep(1000);
      if readbufferCom1(2) then HandoKCom1 := (inbufferCom1[0]=$aa)and(inbufferCom1[1]=$aa);
    end;
    if not HandOKCom1 then CloseHandle(Hcom1);
  end;
  if testflag then ResetLowCom1 := true
  else ResetLowCom1 := HandOKCom1;
end;

procedure CloseCommCom1;
begin
  sleep(1000);
  SendResetCom1(0);
  CloseHandle(hCOM1);
  hcom1 := 0;
end;

//Com2
function GetRValueCom2(ValL,valH:real):real;
var temp : real;
begin
  if ValH<=IHbB then ValH:=IHbB+1;
  if IH0B<=IHbB then IH0B:=IHbB+1;
  temp := (ValH-IHbB)/(IH0B-IHbB) ;
  if temp>0 then temp := ln(temp)
  else temp := 1;
  if ValL<=ILbB then ValL:=ILbB+1;
  if (IL0B<=ILbB)then IL0B:=ILbB+1;
  GetRValueCom2 := ln((ValL-ILbB)/(IL0B-ILbB))/temp;
end;

function GetRhValueCom2(valH:real):real;
begin
  if ValH<=IHbB then ValH:=IHbB+1;
  if IH0B<=IHbB then IH0B:=IHbB+1;
  GetRhValueCom2 := ln((ValH-IHbB)/(IH0B-IHbB));
end;

function GetCValueCom2(valL,valH:real):real;
//function GetCValueCom2(valH,valL:real):real;
var cc,rl,rh:real;
begin
  rl :=  GetRValueCom2(valL,valH);
  rh :=  GetRhValueCom2(valH);

  cc := k2B+k1B*rl+k4B+k3B*rh;
  if cc<0 then cc := 0;
  GetCValueCom2 := cc;
end;

function GetFValueCom2(CVal:real): real;
begin
    //GetFValue := (CVal*(1 + fC)+fB*fV+fD*fM)/0.239;
  {if cval>0 then getFValue := fp1+fp2*fv+fp3*CVal+fp4*fM
  else}
  getFValueCom2 := 0;
end;

procedure SendBufferCom2(Len:Integer);
begin
  WriteFile(hCom2, OutBufferCom2,Cardinal(Len),Cardinal(PPCom2),nil);
end;

Function ReadBufferCom2(Len:Integer) : Boolean;
var k,pp1 : Integer;
    inbuffer1 : array [0..11] of Byte;
begin
  ReadBufferCom2 := false;
  LowResetCom2 := false;
  ReadFile(hCom2, InBufferCom2,Cardinal(Len),Cardinal(ppCom2),nil);
  if ppCom2>0 then begin
    if ppCom2<len then begin
      //sleep(100);
      ReadFile(hCom2,InBuffer1,Cardinal(len-ppCom2),Cardinal(pp1),nil);
      if (ppCom2+pp1)=Len then begin
        for k := 0 to pp1 - 1 do InbufferCom2[k+ppCom2] := Inbuffer1[k];
        ReadbufferCom2 := true;
      end;
    end else ReadBufferCom2 := true;
  end;
end;

procedure SendResetCom2(rf:byte);
begin
  outbufferCom2[0] := $aa;
  outbufferCom2[1] := rf;
  outbufferCom2[2] := 0;
  outbufferCom2[3] := 0;
  outbufferCom2[4] := 0;
  outbufferCom2[5] := $aa;
  sendbufferCom2(6);
end;

procedure SendOfflineCom2;
begin
  outbufferCom2[0] := $bb;
  outbufferCom2[1] := Com2tmdef mod 100;
  outbufferCom2[2] := Com2tmdef div 100;
  outbufferCom2[3] := Com2repdef;
  outbufferCom2[4] := 10;
  outbufferCom2[5] := $bb;
  sendbufferCom2(6);
end;

procedure SendOnLineCom2;
begin
  outbufferCom2[0] := $cc;
  outbufferCom2[1] := 0;  //1 calgs  0 no
  outbufferCom2[2] := 5;  // rep= rebhf[2] senddata
  outbufferCom2[3] := 0;
  outbufferCom2[4] := 0;
  outbufferCom2[5] := $cc;
  sendbufferCom2(6);
end;

procedure SendDAOutCom2(ii,vv:Integer);
begin
  outbufferCom2[0] := $dd;
  outbufferCom2[1] := ii mod 100;
  outbufferCom2[2] := ii div 100;
  outbufferCom2[3] := vv mod 100;
  outbufferCom2[4] := vv div 100;
  outbufferCom2[5] := $dd;
  sendbufferCom2(6);
end;

function ReceiveCountCom2 : Boolean;
begin
  ReceiveCountCom2 := false;
  if (ReadBufferCom2(13)) then begin
    if (InbufferCom2[0]=$ee)and(InbufferCom2[12]=$ee) then begin
      Com2tmind := inbufferCom2[2]*100+inbufferCom2[1];
      Com2repind := inbufferCom2[3];
      Com2CountL[Com2repind] := inbufferCom2[4]*256*256*256+inbufferCom2[5]*256*256
                       +inbufferCom2[6]*256 + inbufferCom2[7];
      Com2CountH[Com2repind] := inbufferCom2[8]*256*256*256+inbufferCom2[9]*256*256
                       +inbufferCom2[10]*256 + inbufferCom2[11];
      if not onlineflagCom2 then begin
        Com2CountL[Com2repind] := round(Com2countL[Com2repind]*60/Com2tmdef);
        Com2CountH[Com2repind] := round(Com2countH[Com2repind]*60/Com2tmdef);
      end;
      ReceiveCountCom2 := true;
      if Com2CountL[Com2repind]=0 then Com2CountL[Com2repind] := 100000;
      if Com2CountH[Com2repind]=0 then Com2CountH[Com2repind] := 200000;
    end else if (InbufferCom2[11]=$ff) then LowResetCom2 := true;
  end;
  if testflag then begin
    Com2CountL[Com2repind] := 1000000+random(100000);
    Com2CountH[Com2repind] := 2000000+random(200000);
    ReceiveCountCom2 := true;
  end;
end;

function InitializeCommCom2(SerialPort:Integer) : Boolean;
var dcb        : TDCB;
    TimeStatus : TCOMMTIMEOUTS;
    InitFlag   : Boolean;
    ComStr     : PChar;
begin
  if SerialPort=0 then ComStr := PChar(Com2);
  hCom2 := CreateFile(COMStr,
                     GENERIC_WRITE or GENERIC_READ,
                     0,
                     nil,
                     OPEN_EXISTING,
                     FILE_ATTRIBUTE_COMPRESSED,
                     0);
  InitFlag := (hCom2<>-1);
  if Initflag then begin
    InitFlag := GetCommState(hCom2,dcb);
    if InitFlag then begin
      dcb.BaudRate := 2400;
      dcb.ByteSize := 8;
      dcb.Parity := NOPARITY;
      dcb.StopBits := ONESTOPBIT;
      InitFlag := SetCommState(HCom2, dcb);
      GetCommTimeouts(HCom2,TimeStatus);
      TimeStatus.ReadTotalTimeoutConstant := 500;
      SetCommTimeouts(HCom2,TimeStatus);
    end;
  end;
  InitializeCommCom2 := InitFlag;
end;

function ResetLowCom2 : Boolean;
begin
  //if hcom<>-1 then closehandle(hcom);
  if hCom2<>0 then closehandle(hCom2);
  handokCom2 := false;
  if InitializeCommCom2(0) then begin
    SendResetCom2(1);
    sleep(1000);
    if readbufferCom2(2) then HandoKCom2 := (inbufferCom2[0]=$aa)and(inbufferCom2[1]=$aa);
    //else CloseHandle(Hcom);
  end;
  if not HandOKCom2 then begin
    if hCom2<>-1 then closehandle(hCom2);
    if InitializeCommCom2(1) then begin
      SendResetCom2(1);
      sleep(1000);
      if readbufferCom2(2) then HandoKCom2 := (inbufferCom2[0]=$aa)and(inbufferCom2[1]=$aa);
    end;
    if not HandOKCom2 then CloseHandle(HCom2);
  end;
  if testflag then ResetLowCom2 := true
  else ResetLowCom2 := HandOKCom2;
end;

procedure CloseCommCom2;
begin
  sleep(1000);
  SendResetCom2(0);
  CloseHandle(hCom2);
  hCom2 := 0;
end;

//Com3
function GetRValueCom3(ValL,valH:real):real;
var temp : real;
begin
  if ValH<=IHbA then ValH:=IHbA+1;
  if IH0A<=IHbA then IH0A:=IHbA+1;
  temp := (ValH-IHbA)/(IH0A-IHbA) ;
  if temp>0 then temp := ln(temp)
  else temp := 1;
  if ValL<=ILbA then ValL:=ILbA+1;
  if (IL0A<=ILbA)then IL0A:=ILbA+1;
  GetRValueCom3 := ln((ValL-ILbA)/(IL0A-ILbA))/temp;
end;

function GetRhValueCom3(valH:real):real;
begin
  if ValH<=IHbA then ValH:=IHbA+1;
  if IH0A<=IHbA then IH0A:=IHbA+1;
  GetRhValueCom3 := ln((ValH-IHbA)/(IH0A-IHbA));
end;


//function GetCValueCom3(valH,valL:real):real;
function GetCValueCom3(valL,valH:real):real; 
var cc,rl,rh:real;
begin
  rl :=  GetRValueCom3(valL,valH);
  rh :=  GetRhValueCom3(valH);

  cc := k2A+k1A*rl+k4A+k3A*rh;
  if cc<0 then cc := 0;
  GetCValueCom3 := cc;
end;

function GetFValueCom3(CVal:real): real;
begin
    //GetFValue := (CVal*(1 + fC)+fB*fV+fD*fM)/0.239;
  {if cval>0 then getFValue := fp1+fp2*fv+fp3*CVal+fp4*fM
  else}
  getFValueCom3 := 0;
end;

procedure SendBufferCom3(Len:Integer);
begin
  WriteFile(hCom3, OutBufferCom3,Cardinal(Len),Cardinal(PPCom3),nil);
end;

Function ReadBufferCom3(Len:Integer) : Boolean;
var k,pp1 : Integer;
    inbuffer1 : array [0..11] of Byte;
begin
  ReadBufferCom3 := false;
  LowResetCom3 := false;
  ReadFile(hCom3, InBufferCom3,Cardinal(Len),Cardinal(ppCom3),nil);
  if ppCom3>0 then begin
    if ppCom3<len then begin
      //sleep(100);
      ReadFile(hCom3,InBuffer1,Cardinal(len-ppCom3),Cardinal(pp1),nil);
      if (ppCom3+pp1)=Len then begin
        for k := 0 to pp1 - 1 do InbufferCom3[k+ppCom3] := Inbuffer1[k];
        ReadbufferCom3 := true;
      end;
    end else ReadBufferCom3 := true;
  end;
end;

procedure SendResetCom3(rf:byte);
begin
  outbufferCom3[0] := $aa;
  outbufferCom3[1] := rf;
  outbufferCom3[2] := 0;
  outbufferCom3[3] := 0;
  outbufferCom3[4] := 0;
  outbufferCom3[5] := $aa;
  sendbufferCom3(6);
end;

procedure SendOfflineCom3;
begin
  outbufferCom3[0] := $bb;
  outbufferCom3[1] := Com3tmdef mod 100;
  outbufferCom3[2] := Com3tmdef div 100;
  outbufferCom3[3] := Com3repdef;
  outbufferCom3[4] := 10;
  outbufferCom3[5] := $bb;
  sendbufferCom3(6);
end;

procedure SendOnLineCom3;
begin
  outbufferCom3[0] := $cc;
  outbufferCom3[1] := 0;  //1 calgs  0 no
  outbufferCom3[2] := 5;  // rep= rebhf[2] senddata
  outbufferCom3[3] := 0;
  outbufferCom3[4] := 0;
  outbufferCom3[5] := $cc;
  sendbufferCom3(6);
end;

procedure SendDAOutCom3(ii,vv:Integer);
begin
  outbufferCom3[0] := $dd;
  outbufferCom3[1] := ii mod 100;
  outbufferCom3[2] := ii div 100;
  outbufferCom3[3] := vv mod 100;
  outbufferCom3[4] := vv div 100;
  outbufferCom3[5] := $dd;
  sendbufferCom3(6);
end;

function ReceiveCountCom3 : Boolean;
begin
  ReceiveCountCom3 := false;
  if (ReadBufferCom3(13)) then begin
    if (InbufferCom3[0]=$ee)and(InbufferCom3[12]=$ee) then begin
      Com3tmind := inbufferCom3[2]*100+inbufferCom3[1];
      Com3repind := inbufferCom3[3];
      Com3CountL[Com3repind] := inbufferCom3[4]*256*256*256+inbufferCom3[5]*256*256
                       +inbufferCom3[6]*256 + inbufferCom3[7];
      Com3CountH[Com3repind] := inbufferCom3[8]*256*256*256+inbufferCom3[9]*256*256
                       +inbufferCom3[10]*256 + inbufferCom3[11];
      if not onlineflagCom3 then begin
        Com3CountL[Com3repind] := round(Com3countL[Com3repind]*60/Com3tmdef);
        Com3CountH[Com3repind] := round(Com3countH[Com3repind]*60/Com3tmdef);
      end;
      ReceiveCountCom3 := true;
      if Com3CountL[Com3repind]=0 then Com3CountL[Com3repind] := 100000;
      if Com3CountH[Com3repind]=0 then Com3CountH[Com3repind] := 200000;
    end else if (InbufferCom3[11]=$ff) then LowResetCom3 := true;
  end;
  if testflag then begin
    Com3CountL[Com3repind] := 1000000+random(100000);
    Com3CountH[Com3repind] := 2000000+random(200000);
    ReceiveCountCom3 := true;
  end;
end;

function InitializeCommCom3(SerialPort:Integer) : Boolean;
var dcb        : TDCB;
    TimeStatus : TCOMMTIMEOUTS;
    InitFlag   : Boolean;
    ComStr     : PChar;
begin
  if SerialPort=0 then ComStr := PChar(Com3);
  hCom3 := CreateFile(COMStr,
                     GENERIC_WRITE or GENERIC_READ,
                     0,
                     nil,
                     OPEN_EXISTING,
                     FILE_ATTRIBUTE_COMPRESSED,
                     0);
  InitFlag := (hCom3<>-1);
  if Initflag then begin
    InitFlag := GetCommState(hCom3,dcb);
    if InitFlag then begin
      dcb.BaudRate := 2400;
      dcb.ByteSize := 8;
      dcb.Parity := NOPARITY;
      dcb.StopBits := ONESTOPBIT;
      InitFlag := SetCommState(HCom3, dcb);
      GetCommTimeouts(HCom3,TimeStatus);
      TimeStatus.ReadTotalTimeoutConstant := 500;
      SetCommTimeouts(HCom3,TimeStatus);
    end;
  end;
  InitializeCommCom3 := InitFlag;
end;

function ResetLowCom3 : Boolean;
begin
  //if hcom<>-1 then closehandle(hcom);
  if hCom3<>0 then closehandle(hCom3);
  handokCom3 := false;
  if InitializeCommCom3(0) then begin
    SendResetCom3(1);
    sleep(1000);
    if readbufferCom3(2) then HandoKCom3 := (inbufferCom3[0]=$aa)and(inbufferCom3[1]=$aa);
    //else CloseHandle(Hcom);
  end;
  if not HandOKCom3 then begin
    if hCom3<>-1 then closehandle(hCom3);
    if InitializeCommCom3(1) then begin
      SendResetCom3(1);
      sleep(1000);
      if readbufferCom3(2) then HandoKCom3 := (inbufferCom3[0]=$aa)and(inbufferCom3[1]=$aa);
    end;
    if not HandOKCom3 then CloseHandle(HCom3);
  end;
  if testflag then ResetLowCom3 := true
  else ResetLowCom3 := HandOKCom3;
end;

procedure CloseCommCom3;
begin
  sleep(1000);
  SendResetCom3(0);
  CloseHandle(hCom3);
  hCom3 := 0;
end;

//Com4
function GetRValueCom4(ValL,valH:real):real;
var temp : real;
begin
  if ValH<=IHbB then ValH:=IHbB+1;
  if IH0B<=IHbB then IH0B:=IHbB+1;
  temp := (ValH-IHbB)/(IH0B-IHbB) ;
  if temp>0 then temp := ln(temp)
  else temp := 1;
  if ValL<=ILbB then ValL:=ILbB+1;
  if (IL0B<=ILbB)then IL0B:=ILbB+1;
  GetRValueCom4 := ln((ValL-ILbB)/(IL0B-ILbB))/temp;
end;

function GetRhValueCom4(valH:real):real;
begin
  if ValH<=IHbB then ValH:=IHbB+1;
  if IH0B<=IHbB then IH0B:=IHbB+1;
  GetRhValueCom4 := ln((ValH-IHbB)/(IH0B-IHbB));
end;

function GetCValueCom4(valL,valH:real):real;
//function GetCValueCom4(valH,valL:real):real;
var cc,rl,rh:real;
begin
  rl :=  GetRValueCom4(valL,valH);
  rh :=  GetRhValueCom4(valH);

  cc := k2B+k1B*rl+k4B+k3B*rh;
  if cc<0 then cc := 0;
  GetCValueCom4 := cc;
end;

function GetFValueCom4(CVal:real): real;
begin
    //GetFValue := (CVal*(1 + fC)+fB*fV+fD*fM)/0.239;
  {if cval>0 then getFValue := fp1+fp2*fv+fp3*CVal+fp4*fM
  else}
  getFValueCom4 := 0;
end;

procedure SendBufferCom4(Len:Integer);
begin
  WriteFile(hCom4, OutBufferCom4,Cardinal(Len),Cardinal(PPCom4),nil);
end;

Function ReadBufferCom4(Len:Integer) : Boolean;
var k,pp1 : Integer;
    inbuffer1 : array [0..11] of Byte;
begin
  ReadBufferCom4 := false;
  LowResetCom4 := false;
  ReadFile(hCom4, InBufferCom4,Cardinal(Len),Cardinal(ppCom4),nil);
  if ppCom4>0 then begin
    if ppCom4<len then begin
      //sleep(100);
      ReadFile(hCom4,InBuffer1,Cardinal(len-ppCom4),Cardinal(pp1),nil);
      if (ppCom4+pp1)=Len then begin
        for k := 0 to pp1 - 1 do InbufferCom4[k+ppCom4] := Inbuffer1[k];
        ReadbufferCom4 := true;
      end;
    end else ReadBufferCom4 := true;
  end;
end;

procedure SendResetCom4(rf:byte);
begin
  outbufferCom4[0] := $aa;
  outbufferCom4[1] := rf;
  outbufferCom4[2] := 0;
  outbufferCom4[3] := 0;
  outbufferCom4[4] := 0;
  outbufferCom4[5] := $aa;
  sendbufferCom4(6);
end;

procedure SendOfflineCom4;
begin
  outbufferCom4[0] := $bb;
  outbufferCom4[1] := Com4tmdef mod 100;
  outbufferCom4[2] := Com4tmdef div 100;
  outbufferCom4[3] := Com4repdef;
  outbufferCom4[4] := 10;
  outbufferCom4[5] := $bb;
  sendbufferCom4(6);
end;

procedure SendOnLineCom4;
begin
  outbufferCom4[0] := $cc;
  outbufferCom4[1] := 0;  //1 calgs  0 no
  outbufferCom4[2] := 5;  // rep= rebhf[2] senddata
  outbufferCom4[3] := 0;
  outbufferCom4[4] := 0;
  outbufferCom4[5] := $cc;
  sendbufferCom4(6);
end;

procedure SendDAOutCom4(ii,vv:Integer);
begin
  outbufferCom4[0] := $dd;
  outbufferCom4[1] := ii mod 100;
  outbufferCom4[2] := ii div 100;
  outbufferCom4[3] := vv mod 100;
  outbufferCom4[4] := vv div 100;
  outbufferCom4[5] := $dd;
  sendbufferCom4(6);
end;

function ReceiveCountCom4 : Boolean;
begin
  ReceiveCountCom4 := false;
  if (ReadBufferCom4(13)) then begin
    if (InbufferCom4[0]=$ee)and(InbufferCom4[12]=$ee) then begin
      Com4tmind := inbufferCom4[2]*100+inbufferCom4[1];
      Com4repind := inbufferCom4[3];
      Com4CountL[Com4repind] := inbufferCom4[4]*256*256*256+inbufferCom4[5]*256*256
                       +inbufferCom4[6]*256 + inbufferCom4[7];
      Com4CountH[Com4repind] := inbufferCom4[8]*256*256*256+inbufferCom4[9]*256*256
                       +inbufferCom4[10]*256 + inbufferCom4[11];
      if not onlineflagCom4 then begin
        Com4CountL[Com4repind] := round(Com4countL[Com4repind]*60/Com4tmdef);
        Com4CountH[Com4repind] := round(Com4countH[Com4repind]*60/Com4tmdef);
      end;
      ReceiveCountCom4 := true;
      if Com4CountL[Com4repind]=0 then Com4CountL[Com4repind] := 100000;
      if Com4CountH[Com4repind]=0 then Com4CountH[Com4repind] := 200000;
    end else if (InbufferCom4[11]=$ff) then LowResetCom4 := true;
  end;
  if testflag then begin
    Com4CountL[Com4repind] := 1000000+random(100000);
    Com4CountH[Com4repind] := 2000000+random(200000);
    ReceiveCountCom4 := true;
  end;
end;

function InitializeCommCom4(SerialPort:Integer) : Boolean;
var dcb        : TDCB;
    TimeStatus : TCOMMTIMEOUTS;
    InitFlag   : Boolean;
    ComStr     : PChar;
begin
  if SerialPort=0 then ComStr := PChar(Com4);
  hCom4 := CreateFile(COMStr,
                     GENERIC_WRITE or GENERIC_READ,
                     0,
                     nil,
                     OPEN_EXISTING,
                     FILE_ATTRIBUTE_COMPRESSED,
                     0);
  InitFlag := (hCom4<>-1);
  if Initflag then begin
    InitFlag := GetCommState(hCom4,dcb);
    if InitFlag then begin
      dcb.BaudRate := 2400;
      dcb.ByteSize := 8;
      dcb.Parity := NOPARITY;
      dcb.StopBits := ONESTOPBIT;
      InitFlag := SetCommState(HCom4, dcb);
      GetCommTimeouts(HCom4,TimeStatus);
      TimeStatus.ReadTotalTimeoutConstant := 500;
      SetCommTimeouts(HCom4,TimeStatus);
    end;
  end;
  InitializeCommCom4 := InitFlag;
end;

function ResetLowCom4 : Boolean;
begin
  //if hcom<>-1 then closehandle(hcom);
  if hCom4<>0 then closehandle(hCom4);
  handokCom4 := false;
  if InitializeCommCom4(0) then begin
    SendResetCom4(1);
    sleep(1000);
    if readbufferCom4(2) then HandoKCom4 := (inbufferCom4[0]=$aa)and(inbufferCom4[1]=$aa);
    //else CloseHandle(Hcom);
  end;
  if not HandOKCom4 then begin
    if hCom4<>-1 then closehandle(hCom4);
    if InitializeCommCom4(1) then begin
      SendResetCom4(1);
      sleep(1000);
      if readbufferCom4(2) then HandoKCom4 := (inbufferCom4[0]=$aa)and(inbufferCom4[1]=$aa);
    end;
    if not HandOKCom4 then CloseHandle(HCom4);
  end;
  if testflag then ResetLowCom4 := true
  else ResetLowCom4 := HandOKCom4;
end;

procedure CloseCommCom4;
begin
  sleep(1000);
  SendResetCom4(0);
  CloseHandle(hCom4);
  hCom4 := 0;
end;



BEGIN
  
END.
